export default class button{
  constructor(love,conf){
    this.love = love
    this.text = conf.text || ""
    this.x = conf.x || 0
    this.y = conf.y || 0
    this.w = conf.w || 100
    this.h = conf.h || 40
    this.frameWidth = conf.frameWidth || love.ui.frameWidth
    this.font = conf.font || love.ui.font
    this.frameColor = conf.frameColor || love.ui.frameColor
    this.fillColor = conf.fillColor || love.ui.fillColor
    this.textColor = conf.textColor || love.ui.textColor
    this.visible = true
    this.onTouch = conf.onTouch ? conf.onTouch.bind(this) : function(){}
    this.onHold = conf.onHold ? conf.onHold.bind(this) : function(){}
    this.holdTimer = 0
    this.holdTime = conf.holdTime || 2000
    this.available = true
  }
  check(x,y){
    return this.x<x && this.x+this.w>x && this.y<y && this.y+this.h>y
  }
  checkTouch(x,y){
    //console.log("pos",x,y)
    let rt = this.check(x,y)
    if (rt){
      this.onTouch()
    }
    return rt
  }
  checkHold(x,y,dt){
    let rt = this.check(x, y)
    if (rt) {
      this.holding = true
      this.holdTimer += dt
      if (this.holdTimer>this.holdTime) {
        this.onHold()
        return true
      }
    } else {
      this.holding = false
      this.holdTimer = 0
    }
  }
  draw(){
    this.love.graphics.setColor(this.fillColor)
    this.love.graphics.rectangle("fill", this.x, this.y, this.w, this.h)
    this.love.graphics.setColor(this.frameColor)
    this.love.graphics.setLineWidth(this.frameWidth)
    this.love.graphics.rectangle("line",this.x,this.y,this.w,this.h)
    this.love.graphics.setColor(this.textColor)
    let fontW = this.font.getWidth(this.text)
    let fontH = this.font.getHeight()
    this.love.graphics.setFont(this.font)
    this.love.graphics.print(this.text,this.x+ (this.w - fontW)/2, this.y + this.h/2 + fontH/2 - this.frameWidth )
  }
}